package ro.gateway.aida.charts;

import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.StandardXYItemRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.XYSeries;
import org.jfree.data.XYSeriesCollection;
import ro.xblue.translator.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Jun 20, 2004
 * Time: 12:14:13 PM
 * To change this template use File | Settings | File Templates.
 */
public class TimeChartRenderer {
	public static void make_linechart(
		  HttpServletRequest request, 
		  ServletContext application,
		  ChartWizzard$ActionForm form)
		  throws IOException, ChartException {
		HttpSession session = request.getSession();

//		System.out.println("creating chart");
		LanguageBean lang_bean = TranslatorUtils.getCurrentLanguage(request, application);
		Translator_DB translator_db = (Translator_DB) application.
			  getAttribute(TranslatorConstants.DB_TRANSLATOR);
		Module_DB tr_module = translator_db.getModule("charts");

		String x_axe = "";
		if (form.x_axe_group_criteria != null) {
			x_axe = tr_module != null ? tr_module.getMessage(lang_bean.language, form.x_axe_group_criteria) : form.x_axe_group_criteria;
			if (x_axe == null) {
				x_axe = form.x_axe_group_criteria;
			}
		} else {
			x_axe = tr_module != null ? tr_module.getMessage(lang_bean.language, form.x_axe_criteria) : form.x_axe_criteria;
			if (x_axe == null) {
				x_axe = form.x_axe_criteria;
			}
		}
		String y_axe = "";
		if (form.y_axe_criteria != null) {
			y_axe = tr_module != null ? tr_module.getMessage(lang_bean.language, form.y_axe_criteria) : form.y_axe_criteria;
			if (y_axe == null) {
				y_axe = form.y_axe_criteria;
			}
		}
//		System.out.println("group axe:" + x_axe);
//		System.out.println("y axe:" + y_axe);

		// dataset
		XYSeries xy_series = new XYSeries(y_axe);
		TimeChartDataMan.populate_xy_dataset(xy_series, lang_bean.language, form);
		XYSeriesCollection xy_dataset = new XYSeriesCollection(xy_series);
		if (xy_series.getItemCount() < 1) {
			throw new ChartException("No data to display");
		}

		SimpleDateFormat date_formater = new SimpleDateFormat("dd-MMM-yyyy", Locale.UK);
		StandardXYItemLabelGenerator tooltipgenerator = new StandardXYItemLabelGenerator(date_formater, NumberFormat.getInstance());

		StandardXYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.LINES +
			  StandardXYItemRenderer.SHAPES, tooltipgenerator);

		XYPlot plot = new XYPlot(xy_dataset, new DateAxis(""), new NumberAxis(""), renderer);

		// chart
		JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT,
			  plot, false);
		chart.setBackgroundPaint(java.awt.Color.white);
		chart.setAntiAlias(true);

		ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
		form.file_name = ServletUtilities.saveChartAsPNG(chart,
			  form.chartwidth, form.chartheight,
			  info, session);

		form.tooltipmap = ChartUtilities.getImageMap(form.file_name, info);

//		System.out.println("created chart:" + form.file_name);
	}
	
}
